package com.br.mytho.components.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TextField extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
	private FlowLayout flowLayout = new FlowLayout(FlowLayout.LEADING, 5, 5);
	
	private JPanel panel = new JPanel();
	
	private String watermark;
	private boolean useWatermark = true;
	private Color watermarkColor = Color.LIGHT_GRAY;
	
	private boolean focusListenerEnabled = true;
	
	private Color textColor = Color.BLACK;
	
	private JTextField text = null;
	private JLabel label = null;
	private JLabel requiredLabel = null;
	
	public TextField() {
		this(null);
	}
	
	public TextField(String label) {
		this(label, null);
	}

	public TextField(String label, String watermark) {
		super();
		this.setLayout(boxLayout);
		
		panel.setLayout(flowLayout);
		
		if (label != null) {
			this.label = new JLabel(label);
			panel.add(this.label);
		}
		
		this.watermark = watermark;
		text = new JTextField();
		text.setColumns(20);
		text.setText(this.watermark);
		this.text.setForeground(watermarkColor);
		text.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent fe) {
				if (focusListenerEnabled) {
					JTextField jtf = (JTextField)fe.getSource();
					TextField tf = (TextField)jtf.getParent().getParent();
					if(!fe.isTemporary() && tf.isEmpty()) {
						tf.text.setText(tf.watermark);
						tf.text.setForeground(watermarkColor);
						tf.useWatermark = true;
					}
				}
			}
			
			@Override
			public void focusGained(FocusEvent fe) {
				if (focusListenerEnabled) {
					JTextField jtf = (JTextField)fe.getSource();
					TextField tf = (TextField)jtf.getParent().getParent();
					if(!fe.isTemporary() & tf.useWatermark) {
						tf.text.setText("");
						tf.text.setForeground(textColor);
						tf.useWatermark = false;
					}
				}
			}
		});
		
		panel.add(this.text);
		
		this.add(panel);
		
		requiredLabel = new JLabel("* Required Field");
		requiredLabel.setForeground(Color.RED);
	}

	
	public boolean isEmpty() {
		boolean isEmpty = false;
		
		String s = this.text.getText();
		isEmpty = s == null | s.trim().length() == 0 | useWatermark;
		
		return isEmpty;
	}
	
	public void focusListenerEnabled(boolean focusListenerEnabled) {
		this.focusListenerEnabled = focusListenerEnabled;
		if (!focusListenerEnabled) {
			this.text.setForeground(textColor);
		}
	}
	
	public boolean isValidField() {
		boolean isValid = false;
		
		if (!isEmpty())
			isValid = true;
		
		if (!isValid) {
			this.add(requiredLabel);
		}
		else {
			this.remove(requiredLabel);
		}
		
		SwingUtilities.updateComponentTreeUI(this);
		
		return isValid;
	}
	
	public void setWidth(int width) {
		this.text.setColumns(width);
	}
}
